package dp;

public class CoinChange322 {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            int min = amount;
            for (int j = 0; j < coins.length; j++) {
                if (i - coins[j] < 0) {
                    continue;
                }
                int before = dp[i - coins[j]];
                if (before != -1 && before < min) {
                    min = before;
                }
            }
            if (min == amount) {
                dp[i] = -1;
            } else {
                dp[i] = min + 1;
            }
        }
        return dp[dp.length - 1];
    }
}
